package org.jeecg.modules.sczt.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.google.gson.JsonObject;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;

import org.jeecg.modules.sczt.entity.Sczt;
import org.jeecg.modules.sczt.entity.SpatialQueryParam;
import org.jeecg.modules.sczt.service.IScztService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 市场主体模块接口
 *
 * @author: scott
 */
@Slf4j
@RestController
@RequestMapping("/cydt/sczt")
@Api(tags = "市场主体分析")
public class ScztController {

    /**
     * 注入书签服务
     */
    @Autowired
    private IScztService scztService;


    /**
     * 根据Sczt获取
     *
     * @param shxydm:shxydm
     * @return Result<Sczt>
     */
    @ApiOperation(value = "根据社会信用代码获取市场主体信息")
    @ApiOperationSupport(order=1)
    @RequestMapping(value = "/getScztByShxydm", method = RequestMethod.GET)
    public Result<Sczt> getScztByShxydm(@RequestParam(name = "shxydm") String shxydm) {

        Result<Sczt> result = new Result<>();
        Sczt sczt = scztService.geScztByShxydm(shxydm);

        if (sczt == null) {
            result.error500("当前企业不存在");
        } else {
            result.setResult(sczt);
            result.setSuccess(true);
        }
        return result;

    }

    /**
     * 获取所有市场主体
     *
     * @return
     */
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
//    @ApiOperation(value = "获取所有市场主体")
    public Result<List<Sczt>> listAll() {
        Result<List<Sczt>> result = new Result<>();
        List<Sczt> list = scztService.listAll();

        if (list == null) {
            result.error500("获取所有市场主体");
        } else {
            result.setResult(list);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 分页获取市场主体信息列表(可按名称查询)
     * @param pageNum：页码
     * @param pageSize：每页记录条数
     * @return
     */
    @RequestMapping(value = "/list" ,method = RequestMethod.GET)
    @ApiOperation(value = "分页获取市场主体信息列表")
    @ApiOperationSupport(order=2)
    public Result  list(@RequestParam(name = "pageNum") Number pageNum,
                                   @RequestParam(name = "pageSize") Number pageSize,
                                    @RequestParam(name = "nsrmc",required = false) String nsrmc) {
        Result result = new Result<>();
        JSONObject jsonObject = scztService.list(pageNum,pageSize,nsrmc);

        if (jsonObject == null) {
            result.error500("获取所有市场主体");
        } else {
            result.setResult(jsonObject);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 分页获取市场主体信息列表
     * @param pageNum：页码
     * @param pageSize：每页记录条数
     * @return
     */
    @RequestMapping(value = "/getScztGeoJson" ,method = RequestMethod.GET)
    @ApiOperation(value = "分页获取市场主体GeoJSON数据")
    @ApiOperationSupport(order=3)
    public Result  getScztGeoJson(@RequestParam(name = "pageNum") Number pageNum,
                        @RequestParam(name = "pageSize") Number pageSize,
                                  @RequestParam(name = "nsrmc",required = false) String nsrmc) {
        Result result = new Result<>();
        JSONObject jsonObject = scztService.list(pageNum,pageSize,nsrmc);

        if (jsonObject == null) {
            result.error500("获取所有市场主体");
        } else {
            result.setResult(jsonObject);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 更新市场主体信息【根据ID】
     * @param sczt
     * @return
     */
    @RequestMapping(value = "/updateSczt", method = RequestMethod.POST)
    @ApiOperation(value = "更新市场主体信息")
    @ApiOperationSupport(order=4)
    @ResponseBody
    public Result updateSczt(@RequestBody Sczt sczt){
        Result result = new Result<>();
        Boolean flag= scztService.updateSczt(sczt);
        if (flag){
            result.setSuccess(true);
            result.setResult(sczt);
        }else{
            result.error500("更新失败！");
        }
        return  result;
    }

    /**
     * 根据空间范围获取市场主体信息
     * @param polygon
     * @return
     */
    @ApiOperation(value = "根据空间范围获取市场主体信息列表")
    @ApiOperationSupport(order=5)
    @RequestMapping(value = "/getScztByPolygon",method = RequestMethod.POST)
    @ResponseBody
    public Result getScztByPolygon(@RequestBody SpatialQueryParam params){
        Result result = new Result<>();
        System.out.println("传入的polygon>>>>>>>>:"+params.getPolygon());

        List<Map> scztFeatureList = scztService.getScztByPolygon(JSONObject.toJSONString(params.getPolygon()));
        List features2=new ArrayList();

        for (int i = 0; i < scztFeatureList.size(); i++) {
            features2.add(JSONObject.parse(scztFeatureList.get(i).get("feature").toString()));
        }
        JSONObject featureCollection= new JSONObject();
        featureCollection.put("type","FeatureCollection");
        featureCollection.put("features",features2);
        result.setResult(featureCollection);
        return result;
    }

    @ApiOperation(value = "根据功能区、主体类型、排行等参数获取企业注册资本TopN前列企业")
    @ApiOperationSupport(order=6)
    @RequestMapping(value = "/getQyZczbTopN",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gnqmc", value = "功能区名称", dataType = "String", paramType = "query",
                    allowableValues = "智能家电产业园,临港加工制造区,中德人才科教区", required = true ),
            @ApiImplicitParam(name = "ztlx", value = "市场主体类型", dataType = "String", paramType = "query",
                    allowableValues = "内资企业,外商投资企业", required =true),
            @ApiImplicitParam(name = "pm", value = "企业注册资本排行", dataType = "Int", required = true ,paramType = "query")
    })
    @ResponseBody
    public  Result getQyZczbTopN( @RequestParam(value = "gnqmc") String gnqmc,@RequestParam("ztlx") String ztlx, @RequestParam("pm") int pm){
        Result result = new Result();
        try {
            List<Map> list = scztService.getQyZczbTopN(gnqmc,ztlx,pm);
            result.setResult(list);
            result.setSuccess(true);
        }
        catch (Error error){
            result.setSuccess(false);
            result.error500("获取企业注册资本TopN前列企业失败！");
        }
        return result;
    }
    @ApiOperation(value = "根据功能区获取市场主体企业数量及各行业的企业数量")
    @ApiOperationSupport(order=6)
    @RequestMapping(value = "/getZcScztQysl",method = RequestMethod.GET)
    @ResponseBody
    public  Result getZcScztQysl(@RequestParam(value = "gnqmc",required = false) String gnqmc){
        Result result = new Result();
        try {
            List<Map> list = scztService.getZcScztQysl(gnqmc);
            result.setResult(list);
            result.setSuccess(true);
        }
        catch (Error error){
            result.setSuccess(false);
            result.error500("获取市场主体统计数量失败！");
        }
        return result;
    }

}
